<?php 
$unit_level = 0;
$unit = '';
$user = User::model()->findByPk(Yii::app()->user->id);
if($this->action->id == 'update'){
$user = User::model()->findByPk($model->id);	
}
$lowerUnit = array();
$ops_unit = array();
$ops_unit_key = '';
if($this->action->id == 'update'){	
	$unit = User::model()->getUnit($user->id);
}else{
switch($user->unit_level){
	case 1:
		$lowerUnit = MtsysCity::model()->findAll('PROVINCE_ID=:PID',array(
			':PID' => $user->unit_id
		));
		//$ops_levels = array('2' => '市级');
		$unit_level = 2;
		$ops_unit_key = 'CITY_ID';
		$unit = MtsysProvince::model()->findByPk($user->unit_id)->PROVINCE_NAME;
		break;
	case 2:
		$lowerUnit = MtsysCounty::model()->findAll('CITY_ID=:CID',array(
			':CID' => $user->unit_id
		));
		//$ops_levels = array('3' => '区县级');
		$unit_level = 3;
		$ops_unit_key = 'COUNTY_ID';
		$city =  MtsysCity::model()->findByPk($user->unit_id);
		$sheng = MtsysProvince::model()->find('PROVINCE_ID=:PID',array(
					':PID' => $city->PROVINCE_ID
		));
		$unit = $sheng->PROVINCE_NAME.'&nbsp;&nbsp;-&nbsp;&nbsp;'.$city->CITY_NAME;
		break;
	case 3:
	case 4:
		$lowerUnit = MtsysBase::model()->findAll('COUNTY_ID=:AID',array(
			':AID' => $user->unit_id
		));
		//$ops_levels = array('4' => '站点');
		$unit_level = 4;
		$ops_unit_key = 'BASE_ID';
		$county =  MtsysCounty::model()->findByPk($user->unit_id);
		$city = MtsysCity::model()->find('CITY_ID=:CID',array(
			':CID' => $county->CITY_ID
		));
		$sheng = MtsysProvince::model()->find('PROVINCE_ID=:PID',array(
			':PID' => $city->PROVINCE_ID
		));
		$unit = $sheng->PROVINCE_NAME.'&nbsp;&nbsp;-&nbsp;&nbsp;'.$city->CITY_NAME.'&nbsp;&nbsp;-&nbsp;&nbsp;'.$county->COUNTY_NAME;
		break;
}
if(empty($lowerUnit)){
	echo "<script>alert('没有可选单位,请先创建单位,再添加用户');window.location.replace('index.php?r=unit/admin');</script>";	
}else{
	switch($user->unit_level){
		case 1:
			$ops_unit = CHtml::listData($lowerUnit,'CITY_ID','CITY_NAME');
			$ops_unit[0] = "请选择市级";		
			break;
		case 2:
			$ops_unit = CHtml::listData($lowerUnit,'COUNTY_ID','COUNTY_NAME');
			$ops_unit[0] = "请选择区县级";
			break;
		case 3:
		case 4:
			$ops_unit = CHtml::listData($lowerUnit,'BASE_ID','BASE_NAME');
			//$ops_unit[0] = "请选择站点";
			break;	
	}
}
echo CHtml::activeHiddenField($model,'unit_level',array('value'=>$unit_level));
}
?>
<div class = "row">

<?php 
echo CHtml::activeLabelEx($model,'unit_id');
echo $unit;
if($this->action->id != 'update'){
echo '&nbsp;&nbsp;-&nbsp;&nbsp;';	

if($user->unit_level < 3){
echo CHtml::dropDownList('User['.$ops_unit_key.']',0, $ops_unit ,array(
	'id'=>$ops_unit_key,			
));
}else{
echo "请选择所属站点：";
echo "<br>";
echo CHtml::checkBoxList('User['.$ops_unit_key.']',0, $ops_unit, array(
	'id'=>'BASE_ID',
));	
}
}
?>

</div>